/** 公共方法
 * 其中CommonJs用于外部js引入，
 * VueCommonJs用于vue内部使用vue.use(其中CommonJs用于外部js引入，VueCommonJs用于vue内部使用vue),直接使用this.方法名
 */
import Vue from 'vue'
const CommonJs = {
  convertBase64UrlToBlob (urlData) {
    if (urlData) {
      var bytes = window.atob(urlData.split(',')[1])
      var ab = new ArrayBuffer(bytes.length)
      var ia = new Uint8Array(ab)
      for (var i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i)
      }
      return new Blob([ab], { type: 'image/png' })
    }
  },
  GetQueryString (name) {
    const after = window.location.hash.split('?')[1]
    if (after) {
      const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
      const r = after.match(reg)
      if (r != null) {
        return decodeURIComponent(r[2])
      } else {
        return null
      }
    } else {
      const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
      const r = window.location.search.substr(1).match(reg)
      // console.log(r)

      if (r != null) {
        return unescape(r[2])
      }
      return null
    }
  },
  httpurl (url, fromwho) {
    var sessionId = sessionStorage.getItem('sid')
    switch (fromwho) {
      case 'auth':
        return 'http://u.mysun365.com/ShiroAuth/' + url + '.shtml?systype=ADGREETING&sessionid=' + sessionId
      case 'authnosid':
        return 'http://u.mysun365.com/ShiroAuth/' + url + '.shtml?systype=ADGREETING'
      case 'test':
        return 'http://192.168.1.102:8080/' + url
      default:
        return 'http://u.mysun365.com/UserCenter/' + url + '?systype=ADGREETING&sessionid=' + sessionId
    }
  },
  catchurl (url) {
    if (url && url.indexOf('http') === -1) {
      return 'http://card.adouyun.com/CloudCard/' + url
    }
    return url
  },
  getRootPath (url) {
    // 获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href
    // 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname
    var pos = curWwwPath.indexOf(pathName)
    // 获取主机地址，如： http://localhost:8083
    var localhostPaht = curWwwPath.substring(0, pos)
    // 获取带"/"的项目名，如：/uimcardprj
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1)
    return (localhostPaht + projectName)
  },
  timeBetween (time) {
    var nms = new Date().getTime()
    var bms = nms - time
    var rtime = ''
    var month = Math.floor(bms / (1000 * 60 * 60 * 24 * 30))
    var d = Math.floor(bms / (1000 * 60 * 60 * 24))
    var h = Math.floor(bms / (1000 * 60 * 60))
    var m = Math.floor(bms / (1000 * 60))
    var s = Math.ceil(bms / (1000))
    if (month > 0) { rtime = `${month}月前` } else {
      if (d > 0) { rtime = `${d}天前` } else {
        if (h > 0) { rtime = `${h}小时前` } else {
          if (m > 0) { rtime = `${m}分钟前` } else {
            rtime = `${s}秒前`
          }
        }
      }
    }
    if (month > 12) {
      rtime = '1年前及更早'
    }
    return rtime
  },
  timeDate (time, type) {
    var date = new Date(time) // 获取一个时间对象
    var year = date.getFullYear() // 获取完整的年份(4位,1970)
    var month = date.getMonth() + 1 // 获取月份(0-11,0代表1月,用的时候记得加上1)
    var day = date.getDate() // 获取日(1-31)
    var hour = date.getHours() // 获取小时数(0-23)

    var min = date.getMinutes() // 获取分钟数(0-59)
    var sec = date.getSeconds() // 获取秒数(0-59)
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    if (hour < 10) {
      hour = '0' + hour
    }
    if (min < 10) {
      min = '0' + min
    }

    if (sec < 10) {
      sec = '0' + sec
    }
    switch (type) {
      case 2:
        var dateHms = year + '-' + month + '-' + day + '  ' + hour + ':' + min + ':' + sec
        return dateHms
      case 3:
        var dateYmdCh = year + '年' + month + '月' + day + '日'
        return dateYmdCh
      case 1:
      default:
        const dateYmd = year + '-' + month + '-' + day
        return dateYmd
    }
  }
}
// const VueCommonJs = function () {
//   Object.entries(CommonJs).forEach(element => {
//     Vue.prototype[element[0]] = element[1]
//   })
// }
// export { CommonJs, VueCommonJs }
Object.entries(CommonJs).forEach(element => {
  Vue.prototype['$' + element[0]] = element[1]
})

